# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=mingw-w64-winpthreads
pkgver=11.0.0
pkgrel=2
pkgdesc="MinGW-w64 winpthreads library"
url="https://mingw-w64.sourceforge.net/"
arch="x86_64 x86"
license="custom"
makedepends="
	mingw-w64-binutils
	mingw-w64-crt
	mingw-w64-gcc-base
	"
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.bz2::https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v$pkgver.tar.bz2"
builddir="$srcdir/mingw-w64-v$pkgver/mingw-w64-libraries/winpthreads"

case "$CARCH" in
x86_64)
	_targets="x86_64-w64-mingw32 i686-w64-mingw32"
	subpackages="$subpackages i686-$pkgname:x32"
	makedepends="
		$makedepends
		i686-mingw-w64-binutils
		i686-mingw-w64-crt
		"
	;;
x86)
	_targets="i686-w64-mingw32"
	;;
esac

build() {
	export CFLAGS="$CFLAGS -O2"
	export CXXFLAGS="$CXXFLAGS -O2"
	export CPPFLAGS="$CPPFLAGS -O2"
	# build scripts rewrite -Wl args and break this
	export LDFLAGS="${LDFLAGS/,-z,pack-relative-relocs}"

	local target
	for target in $_targets; do
		msg "building target: $target"
		(
		local bld="$srcdir"/bld-$target
		mkdir -p "$bld"
		cd "$bld"
		"$builddir"/configure \
			--prefix=/usr/$target \
			--host=$target \
			--enable-static \
			--enable-shared \
			CC=$target-gcc
		make
		)
	done
}

package() {
	local target
	for target in $_targets; do
		make -C "$srcdir"/bld-$target DESTDIR="$pkgdir" install
		$target-strip --strip-unneeded "$pkgdir"/usr/$target/bin/*.dll
	done

	# Licenses
	install -Dm644 "$srcdir"/mingw-w64-v$pkgver/COPYING.MinGW-w64/COPYING.MinGW-w64.txt \
		"$pkgdir"/usr/share/licenses/$pkgname/COPYING.MinGW-w64.txt
	install -Dm644 "$srcdir"/mingw-w64-v$pkgver/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt \
		"$pkgdir"/usr/share/licenses/$pkgname/COPYING.MinGW-w64-runtime.txt
	install -Dm644 "$srcdir"/mingw-w64-v$pkgver/mingw-w64-headers/ddk/readme.txt \
		"$pkgdir"/usr/share/licenses/$pkgname/ddk-readme.txt
}

x32() {
	pkdesc="$pkdesc (i686 hosted)"

	amove usr/i686-w64-mingw32
}

sha512sums="
bc1c9fd9d8593ead9375fcbe40950f06cf7616b94dc676db2793ac9b496fe3a6cc94b5793effda3b752942be0d7d01a1c37a8f221aaf178df0d4eeb0aa6d1f8d  mingw-w64-winpthreads-11.0.0.tar.bz2
"
